/* eslint-disable eqeqeq */
/**
 * 公共过滤器
 */
import Vue from 'vue'
import dict from './dict'

const formatDate = function (time, format = 'YY-MM-DD hh:mm:ss') {
  /**
   *  formatDate(new Date().getTime());//2017-05-12 10:05:44
   *  formatDate(new Date().getTime(),'YY年MM月DD日');//2017年05月12日
   *  formatDate(new Date().getTime(),'今天是YY/MM/DD hh:mm:ss');//今天是2017/05/12 10:07:45
   */
  var newTime
  if (!time) newTime = ''
  else {
    var date = new Date(time)
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var min = date.getMinutes()
    var sec = date.getSeconds()

    // 开个长度为10的数组 格式为 00 01 02 03
    var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
      return '0' + index
    })

    newTime = format
      .replace(/YY/g, year)
      .replace(/MM/g, preArr[month] || month)
      .replace(/DD/g, preArr[day] || day)
      .replace(/hh/g, preArr[hour] || hour)
      .replace(/mm/g, preArr[min] || min)
      .replace(/ss/g, preArr[sec] || sec)
  }

  return newTime
}

// 时间戳过滤(过滤到时分秒)
Vue.filter('dateFilter', value => {
  // console.log('va22222lue', value)
  return formatDate(value)
})

// 时间戳过滤(过滤到年月日)
Vue.filter('dateFilterToDay', value => {
  return formatDate(value, 'YY-MM-DD')
})

Vue.filter('dateFilterToDay2', value => {
  return formatDate(value, 'YY-MM-DD hh:mm:ss')
})

//  服务商报备状态
Vue.filter('statusFilterC', value => {
  let desc = ''
  dict.statusListC.every(item => {
    if (item.code == value) {
      desc = item.desc
      return false
    }
    return true
  })
  return desc
})

//  服务商报备状态-缩写版
Vue.filter('statusFilterC1', value => {
  let desc = ''
  dict.statusListC1.every(item => {
    if (item.code == value) {
      desc = item.desc
      return false
    }
    return true
  })
  return desc
})

//  服务商报备状态-审核记录表
Vue.filter('statusFilterC2', value => {
  let desc = ''
  dict.statusListC2.every(item => {
    if (item.code == value) {
      desc = item.desc
      return false
    }
    return true
  })
  return desc
})

//  竞价报备-竞价报备
Vue.filter('statusListC1_b', value => {
  let desc = ''
  dict.statusListC1_b.every(item => {
    if (item.code == value) {
      desc = item.desc
      return false
    }
    return true
  })
  return desc
})
//  竞价报备-审核记录表
Vue.filter('statusFilterC2_b', value => {
  let desc = ''
  dict.statusListC2_b.every(item => {
    if (item.code == value) {
      desc = item.desc
      return false
    }
    return true
  })
  return desc
})

// 保留两位小数
Vue.filter('priceFilter', value => {
  if (!value) value = 0.0
  else value = (parseFloat(value * 1000) / 100 / 1000).toFixed(2)
  return value
})

// 南北区
Vue.filter('areaFilter', value => {
  let desc = ''
  if (value == 1) desc = '南区'
  else if (value == 2) desc = '北区'
  return desc
})
